﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.Relations.DirectAssociations
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DbEntityRelationDiscoveryOutput:ResultBase
    {
        #region props
        private List<TableReference> _TableRefs;
        /// <summary></summary>
        public List<TableReference> TableRefs
        {
            get
            {
                return this._TableRefs;
            }
            set
            {
                this._TableRefs = value;
            }
        }

        private string _ResultFilePath;
        /// <summary></summary>
        public string ResultFilePath
        {
            get
            {
                return this._ResultFilePath;
            }
            set
            {
                this._ResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        public DbEntityRelationDiscoveryOutput():base(Guid.Empty, Guid.Empty)
        {
            this._TableRefs=new List<TableReference>();
            this._ResultFilePath = string.Empty;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="refs"></param>
        /// <param name="resultFilePath"></param>
        public DbEntityRelationDiscoveryOutput(
            Guid componentID, Guid jobTicketID,
            List<TableReference> refs, string resultFilePath) 
            : base(componentID, jobTicketID)
        {
            this._TableRefs = refs;
            this._ResultFilePath = resultFilePath;
        }
        #endregion

        #region Overrides of ResultBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            if(this.TableRefs !=null && this.TableRefs.Count>0)
            {
                XmlNode tblRefNodes = XmlDataUtil.AddElement(ref xDoc, xNode, "TableReferences");
                foreach(TableReference tblRef in this.TableRefs)
                {
                    XmlNode refNode = XmlDataUtil.AddElement(ref xDoc, tblRefNodes, "TableReference");
                    XmlDataUtil.UpdateAttribute(ref xDoc, refNode, "ParentDb", tblRef.ParentDatabase);
                    XmlDataUtil.UpdateAttribute(ref xDoc, refNode, "ParentSchema", tblRef.ParentSchema);
                    XmlDataUtil.UpdateAttribute(ref xDoc, refNode, "ParentTable", tblRef.ParentTableName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, refNode, "PK", tblRef.ParentColumnName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, refNode, "ChildDb", tblRef.ChildDatabase);
                    XmlDataUtil.UpdateAttribute(ref xDoc, refNode, "ChildSchema", tblRef.ChildSchema);
                    XmlDataUtil.UpdateAttribute(ref xDoc, refNode, "ChildTable", tblRef.ChildTableName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, refNode, "FK", tblRef.ChildColumnName);
                }
            }
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.ResultFilePath);

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                DbEntityRelationDiscoveryOutput output = new DbEntityRelationDiscoveryOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.TableRefs=new List<TableReference>();
                XmlNodeList refNodes = xNode.SelectNodes("TableReferences/TableReference");
                if(refNodes !=null && refNodes.Count>0)
                {
                    foreach(XmlNode refNode in refNodes)
                    {
                        TableReference tblRef=new TableReference(
                            string.Empty,string.Empty, string.Empty, string.Empty);
                        tblRef.ParentDatabase = XmlDataUtil.GetAttributeValue(refNode, "ParentDb", "");
                        tblRef.ParentSchema = XmlDataUtil.GetAttributeValue(refNode, "ParentSchema", "");
                        tblRef.ParentTableName = XmlDataUtil.GetAttributeValue(refNode, "ParentTable", "");
                        tblRef.ParentColumnName = XmlDataUtil.GetAttributeValue(refNode, "PK", "");
                        tblRef.ChildDatabase = XmlDataUtil.GetAttributeValue(refNode, "ChildDb", "");
                        tblRef.ChildSchema = XmlDataUtil.GetAttributeValue(refNode, "ChildSchema", "");
                        tblRef.ChildTableName = XmlDataUtil.GetAttributeValue(refNode, "ChildTable", "");
                        tblRef.ChildColumnName = XmlDataUtil.GetAttributeValue(refNode, "FK", "");
                        output.TableRefs.Add(tblRef);
                    }
                }
                output.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
